<template>
    <div class="list">
        <div class="title">
            <h2><slot></slot></h2>
            <span>{{tasks.length}}</span>
        </div>
        <div v-if="tasks.length === 0" class="notask">暂无数据···</div>
        <div v-else class="tasks">
            <ul>
                <li v-for="item in tasks" :key="item.id">
                    <div class="left-task">
                        <input type="checkbox" class="check" @click="check(item)" v-model="item.check">
                        <div @click="item.exit = true">
                            <input type="text" v-if="item.exit" @blur="save(item)" v-model="item.task">
                            <span v-else>{{item.task}}</span>
                        </div>
                    </div>
                    <span @click="del(i)" class="del">-</span>
                </li>
            </ul>
        </div>
    </div>
</template>
<script setup>
import { useStore } from "vuex";
import { computed, defineProps } from "vue";
let props = defineProps({
    type: String
});
let store = useStore();
let tasks = props.type === "all" ? store.state.tasks : computed(() => store.state.tasks.filter(item => item.state === props.type));
// 监听任务的变化进而修改任务
function check(item) {
    item.state = props.type == "work" ? "finish" : "work";
    item.check = !item.check;
    store.commit("changeTasks", store.state.tasks);
} 

const del = (i) => store.commit("del", i)
const save = (item) => {
    item.exit = false
    store.commit("changeTasks", store.state.tasks);
}
</script>

<style lang="scss" scoped>
ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
.list {
    width: 100%;
    .notask {
        width: 100%;
        background-color: #888;
        color: purple;
        text-align: center;
        line-height: 200px;
    }
    .title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 4px;
        box-sizing: border-box;
    }
    .tasks {
        padding: 0 4px;
        box-sizing: border-box;
    }
    .tasks ul li {
        display: flex;
        height: 40px;
        background-color: #fff;
        border-left: 5px solid green;
        border-radius: 6px;
        margin-bottom: 5px;
        justify-content: space-between;
        padding: 0 10px;
        align-items: center;
        .del {
            width: 30px;
            height: 30px;
            background: #333;
            border-radius: 30px;
            color: #fff;
            text-align: center;
            line-height: 30px;
            font-size: 26px;
        }
    }
    .left-task {
        display: flex;
        align-items: center;
        .check {
            width: 25px;
            height: 25px;
            margin-right: 10px;
        }
    }
}
</style>

